<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="../plugins/select2/select2.css" />
	<link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
	<script src="../plugins/select2/select2.min.js" type="text/javascript"></script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>

                    <div class="box-body">
                  			 <ol class="breadcrumb" id="topTypeId">
                        		<li>
		                        	<a href="/toItem_cat" >顶级分类列表</a>
		                        </li>
	                        </ol>

                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除"  onclick="deleteBatchCat()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" ><i class="fa fa-check"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          
                       		
                        
			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>
									      <th class="sorting">类型模板ID</th>
									     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="dataLists">
			                        <!--查询展示-->

			                      </tbody>
			                  </table>
							<!--<div th:insert="admin/page1.html"></div>-->
			                  <!--数据列表/-->
							<div align="left">
								<nav aria-label="Page navigation">
									<input type="hidden" id="pageNumber" name="pageNumber">
									<input type="hidden" id="totalPage" name="totalPage">
									<div class="page_num_inf">
										<i></i> 一共
										<span id="showPage"></span>页<span id="showCount"></span>条
									</div>
									<ul class="pagination">
										<li>
											<a href="javascript:getPage(1);" aria-label="Previous">
												<span aria-hidden="true">首页</span>
											</a>

											<a href="javascript:getPage(2);" aria-label="Previous">
												<span aria-hidden="true"><-上一页</span>
											</a>
										</li>
									</ul>
									<ul class="pagination" id="pages">

									</ul>
									<ul class="pagination">
										<li>
											<a href="javascript:getPage(3);" aria-label="Next">
												<span aria-hidden="true">下一页-></span>
											</a>
											<a href="javascript:getPage(4);" aria-label="Previous">
												<span aria-hidden="true">尾页</span>
											</a>
										</li>
									</ul>
								</nav>
							</div>
						</div>
                        <!-- 数据表格 /-->
						<input type="hidden" id="getItemCatFatherId">
                     </div>
                    <!-- /.box-body -->
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品分类编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>上级商品分类</td>
		      		<td id="title">
		      		   <!--珠宝 >>  银饰-->
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>商品分类名称</td>
		      		<td><input  class="form-control" placeholder="商品分类名称" id="name" name="name">  </td>
		      	</tr>			  
		      	<tr>
		      		<td>类型模板</td>
		      		<td>	      		
		      		<!--	<input select2 config="options['type_template']" placeholder="商品类型模板" class="form-control" type="text"/>-->
						<select name="template" id="template" style="width: 100%" >
							<option th:each="template : ${tempLateList}" th:value="${template.id}" >[[${template.name}]]</option>
						</select>
		      		</td>		      		      		
		      	</tr>		      	
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="saveItem()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

  <!-- 编辑窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="updateModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="updateModalLabel">商品分类编辑</h3>
			  </div>
			  <!--  <td><input  type="hidden" class="form-control" placeholder="商品分类名称" id="updateId" name="id">  </td>-->
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>上级商品分类</td>
						  <td id="updateTitle">
							  <!--珠宝 >>  银饰-->
						  </td>
					  </tr>
					  <tr>
						  <td>商品分类名称</td>

						  <td><input  class="form-control" placeholder="商品分类名称" id="updateName" name="name">  </td>
					  </tr>
					  <tr>
						  <td>类型模板</td>
						  <td>
							  <!--	<input select2 config="options['type_template']" placeholder="商品类型模板" class="form-control" type="text"/>-->
							  <select name="template" id="selectId" style="width: 100%" >
								  <option th:each="template : ${tempLateList}" th:value="${template.id}" >[[${template.name}]]</option>
							  </select>
						  </td>
					  </tr>
				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateItem()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

  <!--公共获取上级分类id-->

</body>
</html>
<script type="text/javascript">
	$(function () {
		getListItemCat(0);
	})
	//getItemCatListMethod
	function getListItemCat(parentId) {
		$.ajax({
			url : "/itemcat/getListItemCat",
			dataType:"json",
			type:"post",
			data:{pageNumber : $("#pageNumber").val(),pageSize:$("#pageSize").val(),parentId : parentId},
			success:function (result) {
				/*buildPage(result.data)*/
				var list = result.data.list;
				var table = '';
				for (var i = 0; i < list.length; i++) {
					var id1 = list[i].id;
					var name1 = JSON.stringify(list[i].name);
					var parent1 = list[i].parentId;

					table += '<tr>';
					table += '<td><input type="checkbox" name="box" value="' + list[i].id + '"></td>';
					table += '<td>' + list[i].id + '</td>', table += '<td>' + list[i].name + '</td>';
					table += '<td>' + list[i].typeId + '</td>';
					table += '<td class="text-center">';
					table += "<button class='btn bg-olive btn-xs' onclick='getNextItem("+id1+","+parent1+","+name1+")' >查询下级</button>";
					table += '<button class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" onclick="toUpdateItemCat(' + list[i].id + ')">修改</button>';
					table += '</td>';
					table += '</tr>';
				}
				$("#dataLists").html(table);

				$("#showCount").html(result.data.total)
				$("#showPage").html(result.data.pages)

				$("#totalPage").val(result.data.pages)
				$("#totalCount").val(result.data.total)
				$("#pageNumber").val(result.data.pageNumber)

				/*var rowPages = ""
				for (var j = 1; j <= result.data.pages; j++) {
					rowPages += "<li>"
					rowPages += "<a href='javascript:changePageNumber(" + j + ")'>" + j + "</a>"
					rowPages += "</li>"
					$("#pages").html(rowPages);
				}*/

				// 获取ol中的所有a标签
				let li = $("#topTypeId").find("li").find("a");
				// console.log(li);
				let text = "";
				$.each(li,function (i,e) {
					if (i == li.length - 1) {
						// 只有一条数据
						text += e.text;
					} else {
						text += e.text + " >> ";
					}
					// 获取最后一个a标签的id(作为父级id)
					let length = li.length;
					if (i == length - 1) {
						let id = e.id;
						// 创建一个span标签
						let l = document.createElement("span");
						// span标签中添加span
						l.innerHTML = "<span name='buttonAdd' value=" + id + ">" + text + "</span>";
						$("#title").html(l);
						$("#fatherId").val(id1);

					}
				})
			},
			error: function (datas) {
					alert("分类页面加载失败");
			}
		})
	}

	var parent_Id = null;
	function getNextItem(id1,parentId1,name1) {
		$("#pageNumber").val(1);
		console.log(id1,parentId1,name1);
		/*parent_Id = id1;*/
		//添加顶部分类

		//创建一个li标签
		var li = document.createElement("li");
		//给li标签添加属性
		li.innerHTML="<a id='"+id1+"' onclick='addType("+id1+",this)'>"+name1+"</a>";
		//获取父标签
		var type = document.getElementById("topTypeId");
		type.appendChild(li);

		getListItemCat(id1);
	}

	//点击顶部类型执行的方法 并删除元素
	function addType(id1,v) {
		//获取自身的父级标签的下面所以标签并删除
		$(v).parent("li").nextAll().remove();

		/*parent_Id = id1;*/
		getListItemCat(id1);
	}

	function reload(){
		window.location.reload();
	}
	function getPage(flag,parentId) {
		var pageNumber = parseInt($("#pageNumber").val());
		var totalPage = parseInt($("#totalPage").val());

		if (flag == 1){
			if (pageNumber <= 1){
				alert("已经是第一页");
				return;
			}
			$("#pageNumber").val(1);
		}else if (flag == 2) {
			if (pageNumber <= 1){
				alert("已经是第一页");
				return;
			}
			$("#pageNumber").val(pageNumber - 1);
		}else if (flag == 3){
			if (pageNumber >= totalPage){
				alert("已经是最后一页");
				return;
			}
			$("#pageNumber").val(pageNumber + 1);
		} else if (flag == 4) {
			if (pageNumber >= totalPage){
				alert("已经是最后一页");
				return;
			}
			$("#pageNumber").val(totalPage);

		}
		getListItemCat(parentId);
	}

/*	function changePageNumber(obj) {
		$("#pageNumber").val(obj);
		/!*getTheLower(parent_Id);*!/
		getTheLower(parent_Id);
	}*/

	//saveMethod
	function saveItem() {
		if(parent_Id == null){
			var pid = 0;
		}else {
			var pid = parent_Id;
		}
		$.ajax({
			url: "/itemcat/saveTtemcat",
			type: "post",
			dataType: "json",
			async: true,
			data:{name:$("#name").val(),typeId:$("#template").val(),parentId: pid},
			success: function (result) {
				if(result){
					alert("增加成功")
					window.location.reload()
				}else{
					alert("增加失败")
				}
			},
			error: function (datas) {
				alert("规格页面加载失败");
			}
		})
	}

	//ReverseMethod
	function toUpdateItemCat(id) {
		$.ajax({
			url: "/itemcat/toUpdateItemCat/"+id,
			type: "post",
			dataType: "json",
			async: true,
			/*,typeId:templateListJson,parentId: $("#title").innerHTML*/
			success: function (obj) {
				var result = obj.data.tbItemCat;

				$("#updateId").val(result.id)
				$("#updateName").val(result.name)
				//回显模板数据
				var results = (obj.data.typeTemp);
				/*$("#").val(results.name)*/
				$("#selectId").find(`option[value=${results.id}]`).prop("selected",true)

			},
			error: function (datas) {
				alert("分类页面加载失败");
			}
		})
	}

	//updateMethod
	function updateItem() {
		if(parent_Id == null){
			var pid = 0;
		}else {
			var pid = parent_Id;
		}
		$.ajax({
			url: "/itemcat/updateItem",
			type: "post",
			dataType: "json",
			async: true,
			data:{name:$("#updateName").val(),id:$("#updateId").val(),typeId:$("#selectId").val(),parentId: pid},
			success: function (result) {
				if(result){
					alert("修改成功")
					window.location.reload()
				}else{
					alert("修改失败")
				}
			},
			error: function (datas) {
				alert("规格页面加载失败");
			}
		})
	}

	//deleteMethod
	function deleteBatchCat() {
		var ids=[];
		$("[name=box]:checked").each(function (i,e){
			ids.push($(e).val());
		})
		$.ajax({
			url: "/itemcat/deleteBatchCat/"+ids,
			dataType: "json",
			type: "post",
			async: true,
			success: function (result) {
				alert(result.message);
				location.reload();
			},
			error: function (result) {
				alert("系统异常,请联系管理员");
			}
		})
	}
</script>